package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.util.StringUtils;
import com.spa.infrastructure.repository.condition.MassageServiceCondition;
import com.spa.infrastructure.repository.mapper.MassageServiceMapper;
import com.spa.infrastructure.repository.po.MassageRefPO;
import com.spa.infrastructure.repository.po.MassageServicePO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class MassageServiceRepositoryService extends ServiceImpl<MassageServiceMapper, MassageServicePO> {

    @Resource
    private MassageServiceMapper massageServiceMapper;

    public List<MassageServicePO> getMyMessageService(Long messagistId) {
        MPJLambdaWrapper<MassageServicePO> wrapper = JoinWrappers.lambda(MassageServicePO.class)
                .selectAll(MassageServicePO.class)//查询表全部字段
                .leftJoin(MassageRefPO.class, MassageRefPO::getServiceId, MassageServicePO::getId)
                .eq(MassageRefPO::getMassagistId,messagistId)
                .eq(MassageServicePO::getIsDel, WhetherTypeEnum.$0.getCode())
                .orderByDesc(MassageServicePO::getCreateTime);
        return massageServiceMapper.selectJoinList(MassageServicePO.class, wrapper);
    }

    /**
     * 分页查询
     *
     * @author: lig
     * @date: 2023/12/6
     */
    public Page<MassageServicePO> pageList(MassageServiceCondition condition) {
        LambdaQueryWrapper<MassageServicePO> wrapper = wrapper(condition);
        wrapper.in(CollUtil.isNotEmpty(condition.getServiceIds()), MassageServicePO::getId,condition.getServiceIds());
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);

    }

    public List<MassageServicePO> list(MassageServiceCondition condition) {
        LambdaQueryWrapper<MassageServicePO> wrapper = wrapper(condition);
        return list(wrapper);

    }

    /**
     * 获取详情
     *
     * @author: lig
     * @date: 2023/12/6
     */
    public MassageServicePO detailsById(Long id) {
        return super.baseMapper.selectById(id);

    }

    public List<MassageServicePO> getMessageServicePOListByCondition(MassageServiceCondition condition) {
        LambdaQueryWrapper<MassageServicePO> wrapper = wrapper(condition);
        return list(wrapper);
    }

    private LambdaQueryWrapper<MassageServicePO> wrapper(MassageServiceCondition condition) {
        LambdaQueryWrapper<MassageServicePO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollectionUtil.isNotEmpty(condition.getServiceIds()), MassageServicePO::getId, condition.getServiceIds());
        wrapper.like(StringUtils.isNotEmpty(condition.getName()), MassageServicePO::getName, condition.getName());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getIsDel()),MassageServicePO::getIsDel,
                condition.getIsDel());
        return wrapper;
    }

    public Long add(MassageServicePO massageServicePO) {
        massageServiceMapper.insert(massageServicePO);
        return massageServicePO.getId();
    }

    public Long countByCondition(MassageServiceCondition condition) {
        return count(wrapper(condition));
    }

    public void updateByCondition(MassageServicePO po) {
        update(null, updateWrapper(po));
    }
    private LambdaUpdateWrapper<MassageServicePO> updateWrapper(MassageServicePO po) {
        LambdaUpdateWrapper<MassageServicePO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ObjectUtil.isNotNull(po.getId()), MassageServicePO::getId, po.getId());
        updateWrapper.set(ObjectUtil.isNotNull(po.getIsDel()), MassageServicePO::getIsDel, po.getIsDel());
        updateWrapper.set(StringUtils.isNotEmpty(po.getName()), MassageServicePO::getName, po.getName());
        updateWrapper.set(ObjectUtil.isNotNull(po.getDuration()), MassageServicePO::getDuration, po.getDuration());
        updateWrapper.set(ObjectUtil.isNotNull(po.getPrice()), MassageServicePO::getPrice, po.getPrice());
        updateWrapper.set(ObjectUtil.isNotNull(po.getDiscountPrice()), MassageServicePO::getDiscountPrice, po.getDiscountPrice());
        updateWrapper.set(StringUtils.isNotEmpty(po.getCover()), MassageServicePO::getCover, po.getCover());
        updateWrapper.set(StringUtils.isNotEmpty(po.getImg()), MassageServicePO::getImg, po.getImg());
        updateWrapper.set(StringUtils.isNotEmpty(po.getRecommendations()), MassageServicePO::getRecommendations, po.getRecommendations());
        updateWrapper.set(StringUtils.isNotEmpty(po.getSex()), MassageServicePO::getSex, po.getSex());
        updateWrapper.set(StringUtils.isNotEmpty(po.getTargetGroup()), MassageServicePO::getTargetGroup, po.getTargetGroup());
        updateWrapper.set(StringUtils.isNotEmpty(po.getHighlights()), MassageServicePO::getHighlights, po.getHighlights());
        updateWrapper.set(StringUtils.isNotEmpty(po.getHighlightsTar()), MassageServicePO::getHighlightsTar, po.getHighlightsTar());
        updateWrapper.set(StringUtils.isNotEmpty(po.getNote()), MassageServicePO::getNote, po.getNote());
        updateWrapper.set(StringUtils.isNotEmpty(po.getDeclaration()), MassageServicePO::getDeclaration, po.getDeclaration());
        return updateWrapper;
    }
}
